const app = getApp()
import {
  getActivityInfoAndSingUpStatus,
  getUserAllInfo,
  addSignUp,
  decrypt,
  addWxUser
} from "./../../../../server/api"
Page({
  data: {
    scene: [],
    id: '',
    oss_url: app.globalData.oss_url,
    content: {},
    adviserInfo: {},
    userInfo: {},
    Height:""     //这是swiper要动态设置的高度属性
  },
  onLoad: function (options) {
    let scene = decodeURIComponent(options.scene).split(',');
    this.setData({
      scene,
      id: scene[3],
      userInfo: app.globalData.user_info || {}
    }, () => {
      this.get_content()
      this.get_userAllInfo()
    })
  },
  onShareAppMessage: function () {
    let that = this;
    return {
      title: that.data.content['activityName'],
      imageUrl: that.data.content['detailImg1'],
      path: '/pages/gift/show/index',
      success: (res) => {

      }
    }
  },
  imgHeight(e){
      var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
      var imgh=e.detail.height;//图片高度
      var imgw=e.detail.width;//图片宽度
      console.log(imgh,imgw)
      var swiperH=winWid*imgh/imgw + "px"//等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
      this.setData({
        Height:swiperH//设置高度
      })
  },
  get_content() {
    let obj = {}
    obj['signUpOneId'] = this.data.userInfo.oneId
    obj['activityId'] = this.data.id
    getActivityInfoAndSingUpStatus(obj).then(res => {
      if (res.code == 0) {
        res.data['startTime'] = res.data && res.data['startTime'] ? res.data['startTime'].substr(0, 10) : ''
        res.data['endTime'] = res.data && res.data['endTime'] ? res.data['endTime'].substr(0, 10) : ''
        if (res.data['activityContent']) {
          res.data['activityContent'] = this.rich_text_img(res.data['activityContent'])
        }

        res.data['swiper'] = []
        if (res.data['detailImg1']) {
          res.data['swiper'].push(res.data['detailImg1'])
        }
        if (res.data['detailImg2']) {
          res.data['swiper'].push(res.data['detailImg2'])
        }
        if (res.data['detailImg3']) {
          res.data['swiper'].push(res.data['detailImg3'])
        }

        if (res.data['activityType'] == '见面礼') {
          wx.setNavigationBarTitle({
            title: '见面礼'
          })
        }
        this.setData({ content: res.data })
      }
    })
  },

  get_userAllInfo() {
    getUserAllInfo({
      userId: this.data.scene['2'] || ''
    }).then(res => {
      if (res.code == 0) {
        res.data['userName'] = res.data && res.data['userName'] ? res.data['userName'].substr(0, 4) : ""
        this.setData({ adviserInfo: res.data })
      }
    })
  },
  phoneCall(e) {
    wx.makePhoneCall({
      phoneNumber: this.data.adviserInfo.userPhone
    });
  },
  rich_text_img(str) {
    str = str.replace(/\<img/gi, '<img style="width:90%;height:auto;display:block;margin:0 auto"');
    return str
  },
  join_gift(e) {
      wx.navigateTo({
      url: `/pages/gift/join/index?id=${this.data.id}&salesmanId=${this.data.scene['2'] || ''}`,
      })
    },
  
  get_phone(e) {
    let type = e.target.dataset.type
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let obj = {}
      obj['openId'] = this.data.userInfo.openId
      obj['session_key'] = app.globalData.session_key
      obj['encryptedData'] = e.detail.encryptedData
      obj['iv'] = e.detail.iv
      decrypt(obj).then(res => {
        if (res.code == 0) {
          app.globalData.user_info.phone = res.data.phone
          this.setData({ 'userInfo.phone': res.data.phone })

          addWxUser({
            openId: app.globalData.user_info.openId,
            phone: app.globalData.user_info.phone
          }).then(res_phone => {

            if(type == '0') {
              this.receive()
            } else if(type == '1') {
              this.join_gift()
            }
            
          })
        }
      })
    }
  },

  receive() {
   
    let data={
      activityId: this.data.id || '',
      activityName:this.data.content.activityName || '',
      oneId: this.data.userInfo.oneId || '',
      salesmanId: this.data.scene['2'] || '',
      openIdPhone: this.data.userInfo.phone || '',
      signUpPhone: this.data.userInfo.phone || '',
    }
    console.log(data,"----------------领取活动")
    addSignUp(data).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: res.data?`礼品领取成功。系统自动为您发放一个 ${res.data}，请到个人中心『我的券码』中查看`:'领取成功',
          icon: 'none',
          duration: 1500
        })
        
        this.get_content()
      }
      else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  }
})